package com.gxa.hualianeshop.dao.impl;

import com.gxa.hualianeshop.dao.BannerDao;
import com.gxa.hualianeshop.entity.pojo.Banner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author bill
 * @date 2023/7/19 15:40
 */
@Repository
public class BannerDaoImpl implements BannerDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void insertOrUpdate(Banner banner) {
        mongoTemplate.save(banner);
    }

    @Override
    public Banner selectOne(Banner banner) {
        return mongoTemplate.findById(banner.getId(),Banner.class);
    }

    @Override
    public List<Banner> selectAll(Banner banner) {
        Query query = new Query(
                Criteria.where("position")
                .is(banner.getPosition())
        );
        return mongoTemplate.find(query,Banner.class);
    }

    @Override
    public void delete(Banner banner) {
        mongoTemplate.remove(banner);
    }
}
